package com.example.kotlinlearn

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.Toast
import com.example.kotlinlearn.broadcast.BroadcastActivity
import com.example.kotlinlearn.databinding.ActivityMainBinding
import com.example.kotlinlearn.datasave.DataSaveActivity
import com.example.kotlinlearn.jetpack.JetpackActivity
import com.example.kotlinlearn.media.MediaActivity
import com.example.kotlinlearn.notification.NotificationActivity
import com.example.kotlinlearn.premission.PremissionActivity
import com.example.kotlinlearn.service.ServiceActivity
import com.example.kotlinlearn.web.WebActivity

class MainActivity : BaseActivity(){

    private val TAG = "FragmentActivity"

    private lateinit var binding: ActivityMainBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //使用ViewBinding
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val button1:Button = findViewById(R.id.button_1)
        val button2:Button = findViewById(R.id.button_2)
        button1.setOnClickListener {
            binding.TextViewClick.text= "字体发送变化了"
            Toast.makeText(this,"这是一个提示",Toast.LENGTH_SHORT).show()
        }
        button2.setOnClickListener {
            val intent = Intent(this,DialogActivity::class.java)
            startActivity(intent)
        }
        if (savedInstanceState!=null){
            Log.d(TAG, "onCreate: 有信息")
        }
        binding.goRecyclerView.setOnClickListener {
            val intent = Intent(this,RecyclerViewActivity::class.java)
            startActivity(intent)
        }
        binding.goChat.setOnClickListener {
            val intent = Intent(this,ChatActivity::class.java)
            startActivity(intent)
        }
        binding.goBroadcast.setOnClickListener {
            val intent = Intent(this,BroadcastActivity::class.java)
            startActivity(intent)
        }
        binding.goDataSave.setOnClickListener {
            val intent = Intent(this,DataSaveActivity::class.java)
            startActivity(intent)
        }
        binding.goPermission.setOnClickListener {
            val intent = Intent(this,PremissionActivity::class.java)
            startActivity(intent)
        }
        binding.goNotification.setOnClickListener {
            val intent = Intent(this,NotificationActivity::class.java)
            startActivity(intent)
        }
        binding.goMedia.setOnClickListener {
            val intent = Intent(this, MediaActivity::class.java)
            startActivity(intent)
        }
        binding.goService.setOnClickListener {
            val intent = Intent(this, ServiceActivity::class.java)
            startActivity(intent)
        }
        binding.goWeb.setOnClickListener {
            val intent = Intent(this, WebActivity::class.java)
            startActivity(intent)
        }
        binding.goJetpack.setOnClickListener {
            val intent = Intent(this, JetpackActivity::class.java)
            startActivity(intent)
        }
    }

    //活动被销毁时一定会执行的方法
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString("Data","销毁日志")
        Log.d(TAG, "onSaveInstanceState: 销毁活动")
    }

    /**
     * 添加菜单栏
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        return true
    }

    /**
     * 菜单栏点击事件
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.item1->{
                val intent = Intent(this,FullscreenActivity::class.java)
                startActivity(intent)
            }
            R.id.item2->{
                val intent = Intent("TestActivity.START")
                intent.addCategory("dahdahdlahdla")
                intent.putExtra("data","这是从第一个活动中传递过来的数据")
                startActivityForResult(intent,1)
            }
            R.id.item3->{
                val intent = Intent(Intent.ACTION_VIEW)
                intent.data = Uri.parse("http://www.baidu.com")
                startActivity(intent)
            }
            R.id.item4->finish()
        }
        return true
    }

    /**
     * 重写方法，接收上一个活动销毁时传递过来的数据
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            1->if (resultCode== RESULT_OK){
                val data = data?.getStringExtra("data")
                Toast.makeText(this, data,Toast.LENGTH_SHORT).show()
            }
        }
    }
}